package com.ruoyi.basedocument.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.basedocument.domain.BomCategory;
import com.ruoyi.basedocument.service.IBomCategoryService;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.util.BillUtil;
import com.ruoyi.util.F7RenderingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedocument.mapper.BomCoefficientMapper;
import com.ruoyi.basedocument.domain.BomCoefficient;
import com.ruoyi.basedocument.service.IBomCoefficientService;

/**
 * 系数Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-06
 */
@Service
public class BomCoefficientServiceImpl implements IBomCoefficientService 
{
    @Autowired
    private BomCoefficientMapper bomCoefficientMapper;
    @Autowired
    private IBomCategoryService bomCategoryService;
    @Autowired
    private F7RenderingUtil f7RenderingUtil;

    /**
     * 查询系数
     * 
     * @param fid 系数主键
     * @return 系数
     */
    @Override
    public BomCoefficient selectBomCoefficientByFid(String fid)
    {
        BomCoefficient bomCoefficient = bomCoefficientMapper.selectBomCoefficientByFid(fid);
        return bomCoefficient;
    }

    /**
     * 查询系数列表
     * 
     * @param bomCoefficient 系数
     * @return 系数
     */
    @Override
    public List<BomCoefficient> selectBomCoefficientList(BomCoefficient bomCoefficient)
    {
        return bomCoefficientMapper.selectBomCoefficientList(bomCoefficient);
    }

    /**
     * 新增系数
     * 
     * @param bomCoefficient 系数
     * @return 结果
     */
    @Override
    public int insertBomCoefficient(BomCoefficient bomCoefficient)
    {
        format(bomCoefficient);
        String versionNumber = getVersionNumber(bomCoefficient);
        bomCoefficient.setCfversion(versionNumber);
        int i = bomCoefficientMapper.insertBomCoefficient(bomCoefficient);
        String substring = versionNumber.substring(0, versionNumber.length() - 2);
        bomCoefficientMapper.updateVersion(substring);
        return i;
    }

    private void format(BomCoefficient bomCoefficient){
        String bom_wfzbases = BillUtil.getDictLabel("bom_wfzbase");
        if(bomCoefficient.getCfwfzbaseid()!=null&&bom_wfzbases.contains(bomCoefficient.getCfwfzbaseid())){
            String bom_wfzbase = DictUtils.getDictValue("bom_wfzbase", bomCoefficient.getCfwfzbaseid());
            bomCoefficient.setCfwfzbaseid(bom_wfzbase);
        }
        String bom_plants = BillUtil.getDictLabel("bom_plant");
        if(bomCoefficient.getCfplantid()!=null&&bom_plants.contains(bomCoefficient.getCfplantid())){
            String bom_plant = DictUtils.getDictValue("bom_plant", bomCoefficient.getCfplantid());
            bomCoefficient.setCfplantid(bom_plant);
        }
        String bom_seasontypes = BillUtil.getDictLabel("bom_seasontype");
        if(bomCoefficient.getCfseasontypeid()!=null&&bom_seasontypes.contains(bomCoefficient.getCfseasontypeid())){
            String bom_seasontype = DictUtils.getDictValue("bom_seasontype", bomCoefficient.getCfseasontypeid());
            bomCoefficient.setCfseasontypeid(bom_seasontype);
        }
        if(StringUtils.isEmpty(bomCoefficient.getCfcategory())&&StringUtils.isNotEmpty(bomCoefficient.getFnumber())){
            String fnumber = bomCoefficient.getFnumber();
            BomCategory bomCategory = new BomCategory();
            bomCategory.setName(fnumber);
            List<BomCategory> bomCategories = bomCategoryService.selectBomCategoryList2(bomCategory);
            if(bomCategories!=null&&bomCategories.size()!=0){
                bomCategory = bomCategories.get(0);
                bomCoefficient.setFname(bomCategory.getName());
                bomCoefficient.setFnumber(bomCategory.getNumber());
                bomCoefficient.setCfcategory(bomCategory.getId());
            }
        }
    }

    /**
     * 修改系数
     * 
     * @param bomCoefficient 系数
     * @return 结果
     */
    @Override
    public int updateBomCoefficient(BomCoefficient bomCoefficient)
    {
        String versionNumber = getVersionNumber(bomCoefficient);
        bomCoefficient.setCfversion(versionNumber);
        int i = bomCoefficientMapper.updateBomCoefficient(bomCoefficient);
        String substring = versionNumber.substring(0, versionNumber.length() - 2);
        bomCoefficientMapper.updateVersion(substring);
        return i;
    }

    /**
     * 批量删除系数
     * 
     * @param fids 需要删除的系数主键
     * @return 结果
     */
    @Override
    public int deleteBomCoefficientByFids(String[] fids)
    {
        return bomCoefficientMapper.deleteBomCoefficientByFids(fids);
    }

    /**
     * 删除系数信息
     * 
     * @param fid 系数主键
     * @return 结果
     */
    @Override
    public int deleteBomCoefficientByFid(String fid)
    {
        return bomCoefficientMapper.deleteBomCoefficientByFid(fid);
    }

    public String getVersionNumber(BomCoefficient bomCoefficient){
        String fnumber = bomCoefficient.getFnumber();
        String cfseasontypeid = bomCoefficient.getCfseasontypeid();
        String cfwfzbaseid = bomCoefficient.getCfwfzbaseid();
        String VersionNumber = cfwfzbaseid + "-" + cfseasontypeid + "-" + fnumber;
        BomCoefficient bomCoefficient1 = new BomCoefficient();
        bomCoefficient1.setCfversion(VersionNumber);
        List<BomCoefficient> bomCoefficients = selectBomCoefficientList(bomCoefficient1);
        if(bomCoefficients==null||bomCoefficients.size()==0){
            return VersionNumber+"-1";
        }else{
            bomCoefficient1 = bomCoefficients.get(bomCoefficients.size() - 1);
            String cfversion = bomCoefficient1.getCfversion();
            String fid = bomCoefficient.getFid();
            if(fid==null||fid.length()==0){
                return VersionNumber+"-"+(bomCoefficients.size()+1);
            }else{
                if(cfversion==null||cfversion.length()==0){
                    return VersionNumber+"-1";
                }
                if(cfversion.contains(VersionNumber)){
                    return bomCoefficient.getCfversion();
                }else{
                    return VersionNumber+"-"+(bomCoefficients.size()+1);
                }
            }
        }

    }

}
